  function ocell = out_moments_joint( G1 , HH , SDX , ZZ ,CC,  sercell , shocell, in , Y , info ); 
% ==================================================================================== 
% OUT_MOMENTS_JOINT
% 
% Output is the same as OUT_MOMENTS_BOTH but instead reported in a single
% cell for the correlations and autocorrelations 
%
% function ocell = out_moments_joint( G1 , HH , SDX , ZZ , sercell , shocell, Y , in ); 
% 
% Combines the evaluation of moments from three sources 
% 1) DATA 
% 2) SIMULATION 
% 3) MODEL 
% 
% 1) Computed only if Y input (data vector) provided 
% Code combines OUT_MOMENTS.m and OUT_MOMENTS_SIM.m 
% Allows use of same functions although does not permit BP filtered data 
% Hence, will be used only for the observable ZZ matrix used in the
% estimation 


% Inputs 
% ======
% gensys solution to the system in the form 
% y(t) = ZZ*s(t)
% s(t) = G1*s(t-1) + HH*e(t)  V( e(t) ) = SDX'*SDX 
% 
% COMPUTE MOMENTS OF THE SOLUTION FROM GENSYS 
% 
% Compute the std, vcv, correlation, ac(1) and variance decomposition
 %
% 
% If data vector is provided compute the equivalent data moments 
% except for the variance decomposition 
% 
% ----------------------------------
% EXTEND TO BAND-PASS FILTER DATA 
% ----------------------------------
% 
% Alejandro Justiniano 9/07/2005 
% ======================================================================

cucd = cd; 

% Initial Checks 
% ---------------
nz = size( ZZ , 1 ); 
nx = size( SDX , 1 ); 

temp = max( abs( eig(G1) ) ); 
if temp > 0.98  
    warning('Warning! Root of G1 is above 0.98 numerical problems may occur'); 
end 

sercell = sercell(:);
if length( sercell ) ~= nz 
    error('Mistmatch length SERCELL and zmat matrix')
end 

if isempty( shocell ) == 1;
    shocell = fnumcell( 'Shock ', 1:nx ) ;
    shocell = shocell(:);
else
    shocell = shocell(:);
    if length( shocell ) ~= nx
        error('Mistmatch length SHOCELL and SDX HH matrices')
    end
end

% --------------------------------
% Decide whether using DATA or NOT 
% --------------------------------
if nargin < 8 | isempty( in ); 
    in.nrep = 1000; 
end 
    
if nargin < 9 | isempty( Y )
    flag_data = 0 ;
    nr = 2;
    T = 150 ; 
else
    flag_data = 1;
    [T,nvar] = size( Y ); 
    nr = 3;
    if nvar ~= nz
        error('Mistmatch matrix of observables and data')
    end
    clear nvar; 
    
    ycov = cov( Y );
    yvar = ( diag( ycov ) )';
    ystd = sqrt( yvar );
    ycorr = corrcoef( Y );
    yacone = accov( Y , 1);

end 

% ====================
%       Top Rows 
% ===================
if nargin < 10 | isempty( info )  
    flag_info = 0; 
else 
    flag_info = 1; 
end 

% --------------------------------
% Check field settings 
% --------------------------------
[ee,in] = ch_field( in , 'nrep' , 1000 ); 
[ee,in] = ch_field( in , 'Ttake'   , T ); 
[ee,in] = ch_field( in , 'Tdrop'   , 200 ); 

Ttake = in.Ttake;

% -----------------------------------------
% CMAT: top cell with Model information 
% ----------------------------------------
if flag_info == 1 
    ncol = max( 6 , nz + 1 );
    
    cmat=emptycell(10,ncol);
    cmat(1,1)={'MOMENTS'};
    cmat(1,4:5)={'SV?',info.sv };
    cmat(2,1:3)={'Authors:','Specification:','Subfolder:'};
    cmat(3,1:3)={info.auth,info.spec,info.subf};
    opath   = extrsubf( info.outpath , cucd );
    % ========================
    % General
    % ========================
    cmat(5,1:2)={opath,'Output PATH'};
    cmat(6,1:2)={info.fmomname,'Output FILE'};
    cmat(7,1:2)={info.tdate,'Date Today'};
    cmat(8,1:2)={info.priornam,'Prior File'};
    % =================================
    % Sample
    % =================================
    cmat(5,4)={'DATA'};
    cmat(6,4:5)={'Demean?',info.demean};
    cmat(7,4:6)={'Sample',info.start,info.finish};
    cmat(8,4:6)={'Estimation',info.startest,info.finishest};
    cmat(9,1:2)={'Sim Length',num2str( Ttake ) }; 
    cmat(9,4:5)={'Num Rep', num2str( in.nrep ) }; 
    
else 
    
    ncol = max( 4 , nz + 1 );
    cmat=emptycell(3,ncol);
    cmat(1,1:2)={'MOMENTS'};
    cmat(3,1:2)={'Sim Length',num2str( Ttake ) }; 
    cmat(3,3:4)={'Num Rep', num2str( in.nrep ) }; 

end 

% --------------------------------
% BASE: Base Cell for output   %
% --------------------------------
addtop = 3; 

base = emptycell( nr + addtop , nz + 1); 
base(3,2:end) = sercell' ; 
base(2,1) = {'============='}; 

rowsim  = addtop+1; 
rowcell = {' SIM MOD';' STA MOD'}; 

if flag_data == 1 
    rowcell =[{' DATA'};rowcell];
    rowsim  = rowsim + 1; 
end 


% ============================================
% Compute Simulated moments %
% =============================================

TT=Ttake+in.Tdrop;
nrep = in.nrep; 

covmat = zeros( nz , nz , nrep ); 
corrmat = zeros( nz , nz , nrep ); 
aconemat   = zeros( nz , nz , nrep ); 

whandle = waitbar(0,'Computing Simulated Moments'); 

ii=1; 
for ii=1:nrep; 

    waitbar( ii/nrep , whandle ); 
    
    shocks=SDX*randn(nx,TT);

    Ysim=simgensys(shocks,G1,CC,HH,ZZ);
    Ysim=Ysim(:,in.Tdrop+1:end);
    Ysim=Ysim';
    covmat(:,:,ii) = cov( Ysim ); 
    corrmat(:,:,ii) = corrcoef( Ysim ); 
    aconemat(:,:,ii) = accov( Ysim , 1);
    
end 
close(whandle); 

% =========================================================
%                   END OCELL: Simulated Moments 
% =========================================================
ocell = emptycell( 2, ncol ); 
ocell(1,1:2) = {'SIMULATED','MOMENTS'}; 
ocell(2,1:2) = {'=========','========'}; 

pmat = round( nrep*[0.05;0.95] ); 

vcfull = squeeze( median( covmat , 3 ) );
covmat= sort( covmat , 3 ); 
covmat_1 = squeeze( covmat(:,:,pmat(1) ) );
covmat_2 = squeeze( covmat(:,:,pmat(2) ) );

% ==============================
%      Standard Deviation      % 
% ==============================
sdevf   = sqrt( diag( vcfull ) ); 
sdevf_1 = sqrt( diag( covmat_1) ) ; 
sdevf_2 = sqrt( diag( covmat_2) ) ; 
clear covmat*; 

topcell = emptycell(  6 , nz + 1 ); 
topcell(1,1)={'SIM STD'}; 
topcell(1,2:end) = sercell'; 
topcell(2,2:end) = num2cprec( sdevf' );
topcell(3,1) = {'5%'}; 
topcell(3,2:end) = num2cprec( sdevf_1' );
topcell(4,1) = {'95%'}; 
topcell(4,2:end) = num2cprec( sdevf_2' );

topcell(5,1)={'SIM VARIANCE'}; 
topcell(6,2:end) = num2cprec( (diag( vcfull ))' ); 
clear vcfull; 

ocell = merge_cells( ocell, topcell , 1); 
clear topcell; 

stdsim_med = sdevf'; 
clear sdevf*; 

% Autocorrelation 
% ---------------
[aconecell,xtemp] = out_sqmat( aconemat, [] , [] , sercell , [] , 'SIM AUTOCORR' ); 
acsim_med = xtemp.med; 
ocell = merge_cells( ocell , aconecell , 1);
clear xtemp aconemat aconecell; 

[corrcell,xtemp] = out_sqmat( corrmat, [] , [] , sercell , [] , 'SIM CROSSCORR' ); 
corrsim_med = xtemp.med; 

ocell = merge_cells( ocell , corrcell, 1); 
clear corrmat corrcell xtemp; 


% ==================================
% Compute moments implied by model %
% ==================================
[vdcom,vcfull,sdevf,ACzero,ACone]=vardecom_mode(G1,HH,SDX,ZZ); 

% =============================================================
%                 MODEL VARIANCE DECOMPOSITIONS              %
% =============================================================
vcell = emptycell( nz + 3, nx + 1); 
vcell(1,1:2)={'VARIANCE','DECOMPOSTION'}; 
vcell(2,1) = {'MODEL STA'}; 
vcell(2,2)  ={'Shocks'}; 
vcell(3,2:end) = shocell'; 
vcell(3,1) = {'Series'}; 
vcell(4:end,1) = sercell; 
vcell(4:end,2:end) = num2cprec( vdcom ); 

ocell = merge_cells( vcell, ocell,  1); 

% =============================================================
%                           Joint matrices                    %
% =============================================================
if flag_data == 1 
    jcell_1= combine_matcells( sercell , sercell , rowcell, ...
        'ALL AUTOCORR', ' ; ' , yacone , acsim_med , ACone );
    jcell_2 = combine_matcells( sercell , sercell , rowcell, ...
        'ALL CROSSCORR', ' ; ' , ycorr , corrsim_med , ACzero ); 
else 
    jcell_1= combine_matcells( sercell , sercell , rowcell, ...
        'ALL AUTOCORR', ' ; ' , acsim_med , ACone );
    jcell_2 = combine_matcells( sercell , sercell , rowcell, ...
        'ALL CROSSCORR', ' ; ' ,  corrsim_med , ACzero );
end 
ocell = merge_cells( jcell_1 , ocell , 1 ); 
ocell = merge_cells( jcell_2 , ocell , 1 ); 
clear jcell*; 

% =======================================
%        AUTOCORRELATION 
% =======================================
temp_row = strcat( 'AUTOCORR ',rowcell ); 
base( addtop+1:end , 1 ) = temp_row(:); 
clear temp_row; 

base(1,1) = {'AUTOCORRELATIONS ALL'}; 
base( rowsim , 2:end   ) = num2cprec( (diag(acsim_med))') ; 
base( rowsim+1 , 2:end ) = num2cprec( diag( ACone )' );  
if flag_data == 1; 
    base( addtop + 1 ,  2:end ) = num2cprec( diag(yacone) )' ; 
end 

ocell = merge_cells( base , ocell , 1); 

% =======================================
%        STANDARD DEVIATION 
% =======================================
base(1,1) = {'STD ALL '}; 
temp_row = strcat( 'STD ',rowcell ); 
base( addtop+1:end , 1 ) = temp_row(:); 
clear temp_row; 

base( rowsim , 2:end   ) = num2cprec( stdsim_med' ); 
base( rowsim+1 , 2:end ) = num2cprec( ( sqrt( diag( vcfull ) ) )' ); 
if flag_data == 1; 
    base( addtop + 1 ,  2:end ) = num2cprec( ystd' ); 
end 
ocell = merge_cells( base , ocell , 1 ); 
clear base; 

% ======================================
%               Title 
% ======================================
ocell = merge_cells( cmat , ocell , 1 ); 
clear cmat; 
    
disp('Create cell with model moments'); 
disp('For simulated data and stationary model'); 